package com.qianchang.microfinance.view.findings;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.ToastUtil;
import com.google.gson.reflect.TypeToken;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.FindGuideBean;
import com.qianchang.microfinance.bean.FindsBean;
import com.qianchang.microfinance.bean.PageBean;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.base.BaseRecyclerViewFragment;
import com.qianchang.microfinance.view.base.EmptyLayout;
import com.qianchang.microfinance.view.base.RecyclerRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/*****
 *  发现Fragment 页面 2018.8.7
 *
 */

public class FindFragment extends BaseRecyclerViewFragment {

    private static final String TAG = "FindFragment";
    Unbinder unbinder;

    @BindView(R.id.refreshLayout)
    RecyclerRefreshLayout recyclerRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.error_layout)
    EmptyLayout emptyLayout;
    @BindView(R.id.webView_id)
    WebView webView;

    private Context mContext;


    @Override
    protected View onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_find_layout, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        mContext = getContext();
        return rootView;
    }



    @Override
    protected void initView() {
        super.initView();
        setTitle("借钱");
    }


    private void getData(){
        HttpParams params = new HttpParams();
        params.put("reqPageNum", 1);
        params.put("opSource", 1);
        params.put("maxResults", 10);
        DMOKHttpUtils.getInstance().post(mContext, DMConstant.ApiURL.FINDGUIDELIST, params,mCallBack);
    }


    @Override
    public void onItemClick(int position, long itemId) {
        super.onItemClick(position, itemId);
        FindsBean findsBean = (FindsBean)mAdapter.getItems().get(position);
        String mId = findsBean.getId();
        findGuideUR(mId);           //获取引导产品 URL

    }

    //调用外部浏览器打开
    private void initWb(String url){
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        startActivity(intent);
    }




    @Override
    protected void requestData(HttpParams params) {
        params.put("opSource", 1);
        params.put("maxResults", 10);
        if(isFlag){
            DMOKHttpUtils.getInstance().post(mContext, DMConstant.ApiURL.FINDGUIDELIST, params,mCallBack);
        }
        isFlag = true;
    }

    @Override
    protected BaseRecyclerAdapter getRecyclerAdapter() {
        return new MFindsAdapter(mContext);
    }

    @Override
    protected Type getType() {
        return new TypeToken<PageBean<FindsBean>>(){}.getType();
    }




    //获取导流产品URL
    private void findGuideUR(String id){

        HttpParams params = new HttpParams();
        params.put("id",id);

        show();
        DMOKHttpUtils.getInstance().post(getContext(), DMConstant.ApiURL.FIND_GUIDE_URL, params, new RequestCallBack<FindGuideBean>() {

            @Override
            public void onSuccess(JsonResult<FindGuideBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    FindGuideBean findGuideBean = response.getObject();
                    if(findGuideBean != null){
                        String url = findGuideBean.getAppURL();
                        initWb(url);
                    }else {
                        ToastUtil.getInstant().show(getContext(), "数据有误请稍后再试!");
                    }
                }else {
                    ToastUtil.getInstant().show(getContext(), response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(getContext(), msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }

    private boolean isFlag = false;

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);         //友盟统计 Fragment  页面统计
        mAdapter.clear();
        onRequestFinish();
        getData();
    }


    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);       //友盟统计 Fragment  页面统计
        isFlag = true;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if(unbinder != null){
            unbinder.unbind();
        }

        if(webView != null){
            webView.destroy();
            webView = null;
        }
    }

}
